home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / ATAN.C < prev    next >
Text File  |  1986-05-18  |  3KB  |  108 lines

  1. /* 1.0  04-27-84 */
  2. /************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1984        *
  6.  ************************************************************************
  7.  *    Programmmed using the algorithms given in:
  8.  *
  9.  *    Coty, William J., Jr., and Waite, William, SOFTWARE MANUAL FOR
  10.  *    THE ELEMENTARY FUNCTIONS, Prentice-Hall Series in Computational
  11.  *    Mathematics, Prentice-Hall, Inc., Inglewood Cliffs, NJ, 1980,
  12.  *    pp. 194-216.
  13.  *
  14.  *----------------------------------------------------------------------*/
  15.  
  16. #include "defs.h"
  17. #include "stdtyp.h"
  18. #include "errno.h"
  19. #include "mathtyp.h"
  20. #include "mathcons.h"
  21.  
  22. /*----------------------------------------------------------------------*/
  23.  
  24. #define P0     -0.13688768894191926929e+2
  25. #define P1     -0.20505855195861651981e+2
  26. #define P2     -0.84946240351320683534e+1
  27. #define P3     -0.83758299368150059274e+0
  28. #define gP(g)     ((((P3*g P2)*g P1)*g P0)*g)
  29.  
  30. #define Q0     +0.41066306682575781263e+2
  31. #define Q1     +0.86157349597130242515e+2
  32. #define Q2     +0.59578436142597344465e+2
  33. #define Q3     +0.15024001160028576121e+2
  34. #define Q4    +1.0
  35. #define Q(g)     ((((g Q3)*g Q2)*g Q1)*g Q0)
  36.  
  37. /*\p*********************************************************************/
  38.     double
  39. atan2(v, u)        /* computes atan(v, u) = atan(v / u)        */
  40.  
  41. /*----------------------------------------------------------------------*/
  42. double u,v;
  43. {
  44.     double f;
  45.     FAST int c;
  46.  
  47.     if (u IS 0.0)
  48.     {    if (v IS 0.0)
  49.         {    errno = EDOM;
  50.             return 0.0;
  51.         }
  52.         return PIover2;
  53.     }
  54.     c = chrstc(v) - chrstc(u);    /* characteristic of v/u    */
  55.     if (c > MAXEXPG)
  56.         f = PIover2;        /* overflow            */
  57.     else if (c < MINEXPG)
  58.         f = 0.0;        /* underflow            */
  59.     else
  60.     {    f = v / u;
  61.         f = atan(ABS(f));
  62.         if (u < 0.0)
  63.             f = PI - f;
  64.     }
  65.     return (v < 0.0 ? -f : f);
  66. }
  67.  
  68. /*\p*********************************************************************/
  69.     double
  70. atan(x)            /* return trigonometric arctangent of x        */
  71.  
  72. /*----------------------------------------------------------------------*/
  73. double x;
  74. {
  75.     double f, r, g;
  76.     FAST int n;
  77.     LOCAL double A[4] =
  78.     {    0.0,
  79.         PIover6,
  80.         PIover2,
  81.         PIover3,
  82.     };
  83.     
  84.     f = ABS(x);
  85.     if (f > 1.0)
  86.     {    f = 1.0 / f;
  87.         n = 2;
  88.     }
  89.     else
  90.         n = 0;
  91.     if (f > TWOmSQRT3)
  92.     {    f = (((SQRT3m1 * f - 0.5) - 0.5) + f) / (SQRT3 + f);
  93.         ++n;
  94.     }
  95.     if (ABS(f) < FADEOUT)
  96.         r = f;
  97.     else
  98.     {    g = f * f;
  99.         r = f + f *
  100.             gP(g)
  101.             /Q(g);
  102.     }
  103.     if (n > 1)
  104.         r = -r;
  105.     r += A[n];
  106.     return (x < 0.0 ? -r : r);
  107. }
  108.